// src/components/countdown/index.js
import $CountDown from '../../utils/countdown.js'
Component({
  /**
   * 组件的属性列表
   */
  externalClasses: ['ex-class'],
  options: {
    multipleSlots: true
  },
  properties: {
    endTime: {
      type: Number,
      //  value: 0,
      observer: 'countDown'
    },
    startText: {
      type: String,
      value: ''
    },
    endText: {
      type: String,
      value: ''
    },
    icon: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    days: 0,
    hours: 0,
    min: 0,
    sec: 0,
    ready: false
  },
  // ready: function() {
  //   this.countDown()
  // },

  /**
   * 组件的方法列表
   */
  methods: {
    countDown() {
      const self = this
      new $CountDown({
        date: new Date(self.properties.endTime),
        render(date) {
          const days = this.leadingZeros(date.days, 2)
          const hours = this.leadingZeros(date.hours, 2)
          const min = this.leadingZeros(date.min, 2)
          const sec = this.leadingZeros(date.sec, 2)
          if(days == 0 && hours == 0 && min == 0  && sec == 0){
            self.triggerEvent('over')
            return
          }
          self.setData({
            days: days,
            hours: hours,
            min: min,
            sec: sec,
            ready: true // 数据转换成功后显示
          })
        }
      })
    },

  }
})
